#include "ModelOutlierRemovalModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.model_outlier_removal_model.json";
}

ModelOutlierRemovalModel::ModelOutlierRemovalModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/model_outlier_removal.html");
}

ModelOutlierRemovalModel& ModelOutlierRemovalModel::get()
{
    static ModelOutlierRemovalModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void ModelOutlierRemovalModel::fromJson(const QJsonObject& json, QStringList& errorList)
{
    BasePclModel::fromJson(json, errorList);

    pcdFile_ = json["pcdFile"].toString(pcdFile_);
    sampleSize_ = json["sampleSize"].toInt(sampleSize_);
    sampleMinPos_ = json["sampleMinPos"].toDouble(sampleMinPos_);
    sampleMaxPos_ = json["sampleMaxPos"].toDouble(sampleMaxPos_);

    sphereX_ = json["sphereX"].toDouble(sphereX_);
    sphereY_ = json["sphereY"].toDouble(sphereY_);
    sphereZ_ = json["sphereZ"].toDouble(sphereZ_);
    sphereRadius_ = json["sphereRadius"].toDouble(sphereRadius_);
    filterThreshold_ = json["filterThreshold"].toDouble(filterThreshold_);
}

QJsonObject ModelOutlierRemovalModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();

    json.insert("pcdFile", QJsonValue(pcdFile_));
    json.insert("sampleSize", QJsonValue(sampleSize_));
    json.insert("sampleMinPos", QJsonValue(sampleMinPos_));
    json.insert("sampleMaxPos", QJsonValue(sampleMaxPos_));
    json.insert("sphereX", QJsonValue(sphereX_));
    json.insert("sphereY", QJsonValue(sphereY_));
    json.insert("sphereZ", QJsonValue(sphereZ_));
    json.insert("sphereRadius", QJsonValue(sphereRadius_));
    json.insert("filterThreshold", QJsonValue(filterThreshold_));

    return json;
}
